package com.xmg.p2p.base.service.impl;

import com.xmg.p2p.base.domain.Account;
import com.xmg.p2p.base.domain.Iplog;
import com.xmg.p2p.base.domain.Logininfo;
import com.xmg.p2p.base.domain.Userinfo;
import com.xmg.p2p.base.mapper.IplogMapper;
import com.xmg.p2p.base.mapper.LogininfoMapper;
import com.xmg.p2p.base.service.IAccountService;
import com.xmg.p2p.base.service.ILogininfoService;
import com.xmg.p2p.base.service.IUserinfoService;
import com.xmg.p2p.base.util.BidConst;
import com.xmg.p2p.base.util.MD5;
import com.xmg.p2p.base.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * lcd  2020/5/6
 * Description:
 */
@Service
public class LogininfoServiceImpl implements ILogininfoService {
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private IUserinfoService userinfoService;
    @Autowired
    private IAccountService accountService;
    @Autowired
    private IplogMapper iplogMapper;
    @Override
    public void register(String username, String password) {
        int count = logininfoMapper.countByUsername(username);
        System.out.println(count);
        if(count<=0){
            Logininfo logininfo = new Logininfo();
            logininfo.setUsername(username);
            logininfo.setPassword(MD5.encode(password));
            logininfo.setState(Logininfo.STATE_NORMAL);
            logininfo.setUserType(Logininfo.USER_CLIENT);
            logininfoMapper.insert(logininfo);
            //同时要添加Userinfo和Account,拥有相同的主键
            Account account = new Account();
            account.setId(logininfo.getId());
            accountService.add(account);

            Userinfo userinfo = new Userinfo();
            userinfo.setId(logininfo.getId());
            userinfoService.add(userinfo);
        }else {
            throw new RuntimeException("用户名已存在");
        }
    }

    @Override
    public boolean checkUsername(String username) {
        return logininfoMapper.countByUsername(username)>0;
    }

    @Override
    public Logininfo login(String username, String password,String ip,int userType) {
        Logininfo login = logininfoMapper.login(username, MD5.encode(password),userType);
        Iplog iplog = new Iplog();
        iplog.setIp(ip);
        iplog.setUserName(username);
        iplog.setLoginTime(new Date());
        iplog.setUserType(userType);
        if (login != null) {
            //存放到UserContext
            UserContext.setLogininfo(login);
            iplog.setState(Iplog.STATE_SUCCESS);
        }else {
            iplog.setState(Iplog.STATE_FAILED);
        }
        iplogMapper.insert(iplog);
        return login;
    }

    @Override
    public void initAdmin() {
        int count=logininfoMapper.countByAdmin();
        if(count==0){
            Logininfo admin = new Logininfo();
            admin.setUsername(BidConst.DEFAULT_ADMIN_NAME);
            admin.setPassword(MD5.encode(BidConst.DEFAULT_ADMIN_PASSWORD));
            admin.setState(Logininfo.STATE_NORMAL);
            admin.setUserType(Logininfo.USER_MANAGER);
            logininfoMapper.insert(admin);

        }
    }
}
